import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import plotly.express as px
df = pd.read_csv("data/NCRB_ADSI-2020_Table_1A.9.csv")
df = df[(df['Category']=='State') + (df['Category']=='Union Territories')]
df = df.sort_values(by='State/UT/City', axis=0)
df.head()
| Si. No. | Category | State/UT/City | Dangerous or Careless Driving/ Over-taking/etc. - Cases | Dangerous or Careless Driving/ Over-taking/etc. - Injured | Dangerous or Careless Driving/ Over-taking/etc. - Died | Over Speeding - Cases | Over Speeding - Injured | Over Speeding - Died | Driving under Influence of Drug/Alcohol - Cases | ... | Other Causes - Died | Total Road Accidents - Cases | Total Road Accidents - Injured | Total Road Accidents - Died | Unmanned Railway Crossing Accidents - Cases | Unmanned Railway Crossing Accidents - Injured | Unmanned Railway Crossing Accidents - Died | Grand Total - Cases | Grand Total - Injured | Grand Total - Died | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 29 | 29 | Union Territories | A & N Islands | 82 | 90 | 8 | 26 | 31 | 2 | 33 | ... | 0 | 141 | 145 | 14 | 0 | 0 | 0 | 141 | 145 | 14 |
| 0 | 1 | State | Andhra Pradesh | 3300 | 3630 | 1221 | 12344 | 13774 | 4778 | 154 | ... | 668 | 17924 | 19675 | 7039 | 0 | 0 | 0 | 17924 | 19675 | 7039 |
| 1 | 2 | State | Arunachal Pradesh | 25 | 15 | 15 | 39 | 32 | 23 | 0 | ... | 19 | 159 | 143 | 90 | 0 | 0 | 0 | 159 | 143 | 90 |
| 2 | 3 | State | Assam | 1169 | 893 | 462 | 3293 | 2530 | 1377 | 491 | ... | 33 | 6737 | 5065 | 2813 | 1 | 0 | 1 | 6738 | 5065 | 2814 |
| 3 | 4 | State | Bihar | 2564 | 1936 | 1881 | 3674 | 3072 | 2785 | 152 | ... | 150 | 8639 | 7019 | 6698 | 0 | 0 | 0 | 8639 | 7019 | 6698 |
5 rows × 51 columns
from urllib.request import urlopen
import json
# with urlopen('https://raw.githubusercontent.com/Subhash9325/GeoJson-Data-of-Indian-States/master/Indian_States') as response:
# counties = json.load(response)
with urlopen('https://raw.githubusercontent.com/nikhilkumarsingh/choropleth-python-tutorial/master/states_india.geojson') as response:
counties = json.load(response)
print(counties["features"][0].keys())
# Note that the loaded geoJSON must have an outermost key id. Note key id is not present here
dict_keys(['type', 'geometry', 'properties'])
' Note that the loaded geoJSON must have an outermost key id. Note key id is not present here'
for i in range(len(counties['features'])):
# Loop to add 'id' key
counties['features'][i]['id']=counties['features'][i]['properties']['st_nm']
print(counties["features"][0].keys())
dict_keys(['type', 'geometry', 'properties', 'id'])
# print(counties['features'][0])
# print(counties['features'][0]['properties']['NAME_1'])
names = []
for i in range(len(counties['features'])):
names.append(counties['features'][i]['id'])
# names.append('NA')
# print(df)
# print(len(counties['features']))
# print(df['State/UT/City'])
names.sort()
df['name2geo']=names
df[['State/UT/City', 'name2geo']]
# df.head()
| State/UT/City | name2geo | |
|---|---|---|
| 29 | A & N Islands | Andaman & Nicobar Island |
| 0 | Andhra Pradesh | Andhra Pradesh |
| 1 | Arunachal Pradesh | Arunanchal Pradesh |
| 2 | Assam | Assam |
| 3 | Bihar | Bihar |
| 30 | Chandigarh | Chandigarh |
| 4 | Chhattisgarh | Chhattisgarh |
| 31 | D & N Haveli and Daman & Diu | Dadara & Nagar Havelli |
| 32 | Delhi (UT) | Daman & Diu |
| 5 | Goa | Goa |
| 6 | Gujarat | Gujarat |
| 7 | Haryana | Haryana |
| 8 | Himachal Pradesh | Himachal Pradesh |
| 33 | Jammu & Kashmir | Jammu & Kashmir |
| 9 | Jharkhand | Jharkhand |
| 10 | Karnataka | Karnataka |
| 11 | Kerala | Kerala |
| 34 | Ladakh | Lakshadweep |
| 35 | Lakshadweep | Madhya Pradesh |
| 12 | Madhya Pradesh | Maharashtra |
| 13 | Maharashtra | Manipur |
| 14 | Manipur | Meghalaya |
| 15 | Meghalaya | Mizoram |
| 16 | Mizoram | NCT of Delhi |
| 17 | Nagaland | Nagaland |
| 18 | Odisha | Odisha |
| 36 | Puducherry | Puducherry |
| 19 | Punjab | Punjab |
| 20 | Rajasthan | Rajasthan |
| 21 | Sikkim | Sikkim |
| 22 | Tamil Nadu | Tamil Nadu |
| 23 | Telangana | Telangana |
| 24 | Tripura | Tripura |
| 25 | Uttar Pradesh | Uttar Pradesh |
| 26 | Uttarakhand | Uttarakhand |
| 27 | West Bengal | West Bengal |
fig = px.choropleth_mapbox(df, geojson=counties, locations='name2geo',
color='Dangerous or Careless Driving/ Over-taking/etc. - Died',
color_continuous_scale="Viridis",
mapbox_style="carto-positron",
zoom=3, center = {"lat": 20.5937, "lon": 78.9629},
opacity=0.5
)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()